package com.yyx.app1.Question_function.UI;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.yyx.app1.MyApp;
import com.yyx.app1.My_function.UI.Login_Activity_;
import com.yyx.app1.Question_function.Control.QuestionListView.ListView_Question_Adapter;
import com.yyx.app1.R;
import com.yyx.app1.ui.BottomMenu_Fragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.FragmentById;
import org.androidannotations.annotations.ViewById;

/**
 * Created by yuexing on 2015/9/15.
 */
@EActivity(R.layout.activity_question)
public class Question_Acitivity extends Activity {
    private ListView_Question_Adapter adapter;

    @ViewById
    LinearLayout line;

    @ViewById
    ListView activity_message_listview;

    @ViewById
    ImageView top_menu_ico;

    @FragmentById
    Fragment activity_message_bottommeun;

    @AfterViews
    public void after()
    {
        if(((MyApp)getApplication()).getBossInfo()==null)
        {
            Intent intent = new Intent(Question_Acitivity.this, Login_Activity_.class);
            intent.putExtra("isback",true);
            startActivity(intent);
        }
        else {
            init();
        }
    }

    @Override
    protected void onRestart() {
        if (adapter==null)
        {
            init();
        }
        super.onRestart();
    }

    private void init()
    {
        adapter = new ListView_Question_Adapter(this);
        top_menu_ico.setImageBitmap(((MyApp) getApplication()).getBaseDataManager().getResDrawable(
                R.drawable.whitethreeline, top_menu_ico.getWidth(), top_menu_ico.getHeight()));
        ((BottomMenu_Fragment) activity_message_bottommeun).setShow("问诊");
        adapter.findAll();
        activity_message_listview.setAdapter(adapter);
        activity_message_listview.setDividerHeight(0);
        activity_message_listview.setOverScrollMode(View.OVER_SCROLL_NEVER);
    }

    @Override
    protected void onDestroy() {
        try
        {
            if (((MyApp)getApplication()).getBossInfo()!=null) {
                top_menu_ico.setImageDrawable(null);
                line.setBackground(null);
                adapter.delete();
            }
            System.gc();
        }
        catch (Exception e)
        {
            Log.e("异常1510291541",e.toString());
        }
        super.onDestroy();
    }

    @Click
    public void line()
    {
        activity_message_listview.setSelection(0);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            finish();
        }
        return true;
    }
}
